import { getCategoryList} from '../../api/store.js';
import { CACHE_LONGITUDE, CACHE_LATITUDE} from '../../config.js';
import { storeListApi } from '../../api/store.js';
import wxh from '../../utils/wxh.js';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navlist: [],
    productList: [],
    navActive: 0,
    parameter: {
      'navbar': '1',
      'return': '0',
      'title':'产品分类'
    },
    navH:"",
    number:"",
    onpage:1,
    loading: false,//是否加载中
    loadend: false,//是否加载完毕
    loadTitle: '加载更多',//提示语
    page: 1,
    limit: 10,
    isClose: false,
    storeList: [],
    status: '',
    store_id:1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  navclick:function(e){
  var page= e.currentTarget.dataset.page
 this.setData({
    onpage:page
  })
  if(page==1){
    this.getAllCategory();
  }else if(page==2){
    let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
    let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
    console.log(longitude)
    console.log(latitude)
    // this.setData({ latitude: latitude, longitude: longitude });
    if (longitude && latitude){
      this.getList(longitude, latitude);
    }else{
      this.selfLocation();
    }
  }



  },
  onLoad: function (res) {
    this.getAllCategory();
    // 店铺
    let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
    let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
    // this.setData({ latitude: latitude, longitude: longitude });
    if (longitude && latitude){
      this.getList(longitude, latitude);
    }else{
      this.selfLocation();
    }
  },



/**
  * 授权地址
  * 
  */
 selfLocation: function () {
  const that = this;
  wxh.selfLocation().then(res=>{
    that.getList(res.longitude, res.latitude);
  }).catch(()=>{
    that.getList();
  });
},
/**
* 选中门店
* 
*/
checked: function (e) {
  let details = e.currentTarget.dataset.details;
  let pages = getCurrentPages();   //当前页面
  let prevPage = pages[pages.length - 2];   //上一页面
  prevPage.setData({
    storeItem: details
  });
  if (this.data.status === 'order') wx.navigateBack({ delta: 1 });
},
/**
  * 获取门店列表数据
 */
getList: function (longitudes, latitudes) {
  console.log(11)
  // if (this.data.loadend) return;
  // if (this.data.loading) return;
  // this.setData({ loading: true, loadTitle: "" });
  let data={
    latitude: latitudes || '', //纬度
    longitude: longitudes || '', //经度
    page: this.data.page,
    limit: this.data.limit,
    store_id: this.data.store_id
  }
  storeListApi(data).then(res => {
    console.log(res)
    let list = res.data.list || [];
    let loadend = list.length < this.data.limit;
    // this.data.storeList = app.SplitArray(list, this.data.storeList);
    this.setData({
      storeList: list,
      loadend: loadend,
      loading: false,
      loadTitle: loadend ? "我也是有底线的" : '加载更多',
      // page: this.data.page + 1
    });
  }).catch(err => {
    this.setData({ loading: false, loadTitle: "加载更多" });
  })
},
/**
 * 拨打电话
*/
makePhone: function (e) {
  let phone = e.currentTarget.dataset.phone;
  wx.makePhoneCall({
    phoneNumber: phone
  })
},
/**
* 打开地图
* 
*/
showMaoLocation: function (e) {
  let details = e.currentTarget.dataset.details;
  if (!details.latitude || !details.longitude) return app.Tips({ title: '缺少经纬度信息无法查看地图！' });
  wx.openLocation({
    latitude: parseFloat(details.latitude),
    longitude: parseFloat(details.longitude),
    scale: 8,
    name: details.name,
    address: details.address + details.detailed_address,
    success: function () {

    },
  });
},



  // 产品
  infoScroll:function(){
    let that = this;
    let len = that.data.productList.length;
    that.setData({
      navH: app.globalData.navHeight,
      number: that.data.productList[len - 1].children.length
    })
    //设置商品列表高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: (res.windowHeight) * ( 750/res.windowWidth) - 98 - app.globalData.navHeight
          //res.windowHeight:获取整个窗口高度为px，*2为rpx；98为头部占据的高度；
        })
      },
    });
    var height = 0;
    var hightArr = [];
    for (var i = 0; i < len; i++) { //productList
      //获取元素所在位置
      var query = wx.createSelectorQuery().in(this);
      var idView = "#b" + i;
      query.select(idView).boundingClientRect();
      query.exec(function (res) {
        var top = res[0].top;
        hightArr.push(top);
        that.setData({
          hightArr: hightArr
        });
      });
    };
  },
  tap: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    this.setData({
      toView: id,
      navActive: index
    });
  },
  getAllCategory:function(){
    var that = this;
    getCategoryList().then(res=>{
      that.setData({
        productList: res.data
      });
      that.infoScroll();
    })
  },
  scroll: function (e) {
    var scrollTop = e.detail.scrollTop;
    var scrollArr = this.data.hightArr;
    for (var i = 0; i < scrollArr.length; i++) {
      if (scrollTop >= 0 && scrollTop < scrollArr[1] - scrollArr[0]) {
        this.setData({
          navActive: 0,
          lastActive: 0
        })
      } else if (scrollTop >= scrollArr[i] - scrollArr[0] && scrollTop < scrollArr[i + 1] - scrollArr[0]) {
        this.setData({
          navActive: i
        })
      } else if (scrollTop >= scrollArr[scrollArr.length - 1] - scrollArr[0]) {
        this.setData({
          navActive: scrollArr.length - 1
        })
      }
    }
  },

  searchSubmitValue: function (e) {
    if (e.detail.value.length > 0) 
      wx.navigateTo({ url: '/pages/goods_list/goods_list?searchValue=' + e.detail.value})
    else 
      return app.Tips({ title:'请填写要搜索的产品信息'});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
    let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
    this.getList(longitude, latitude);
  }
})